/**
* @fileOverview 常用图片操作
* @author <a href=”www.liujto.com”>liujto</a>
* @createDate  2019-10-10
* @copyright https://www.liujto.com
* @version 1.0
*/

import config from '@/config.js'

 /**
  * 压缩图片
  * @param {Base64String} _imgbase64 要压缩的图片
  */
function CompressImg(_imgbase64) {
    var img = dataURLtoFile(_imgbase64, 'img')
        // console.log('转换的图片', img)
    let canvas = document.createElement("canvas");
    let ctx = canvas.getContext("2d");
    let initSize = img.src.length;
    let width = img.width;
    let height = img.height;
    canvas.width = width;
    canvas.height = height;
    // 铺底色
    ctx.fillStyle = "#fff";
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    ctx.drawImage(img, 0, 0, width, height);

    //进行最小压缩
    let ndata = canvas.toDataURL("image/jpeg", 0.1);
    return ndata;
}
/**
 * base64转file
 * @param {Base64String} dataurl 要转换的图片
 * @param {String} filename 图片名称
 */
function dataURLtoFile(dataurl, filename) {
    var arr = dataurl.split(',');
    var bytes = window.atob(arr[1]); //去掉url的头，并转换为byte  
    var mime = arr[0].match(/:(.*?);/)[1]; // 结果：   image/png
    //处理异常,将ascii码小于0的转换为大于0  
    var u8arr = new ArrayBuffer(bytes.length);
    var ia = new Uint8Array(u8arr);
    for (var i = 0; i < bytes.length; i++) {
        ia[i] = bytes.charCodeAt(i);
    }
    return new File([u8arr], filename, { type: mime });

    // var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
    //     bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
    // while(n--){
    //     u8arr[n] = bstr.charCodeAt(n);
    // }
    // return new File([u8arr], filename, {type:mime});
}
/**
 * base64转blob
 * @param {Base64String} base64Data 要转换的图片
 */
function dataURItoBlob(base64Data) {
    var byteString;
    if (base64Data.split(",")[0].indexOf("base64") >= 0)
        byteString = atob(base64Data.split(",")[1]);
    else byteString = unescape(base64Data.split(",")[1]);
    var mimeString = base64Data
        .split(",")[0]
        .split(":")[1]
        .split(";")[0];
    var ia = new Uint8Array(byteString.length);
    for (var i = 0; i < byteString.length; i++) {
        ia[i] = byteString.charCodeAt(i);
    }
    return new Blob([ia], { type: mimeString });
}
/**
 * 判断图片是否存在
 * @param {String} imgurl 图片地址
 */
function CheckImgExists(imgurl) {
    var ImgObj = new Image(); //判断图片是否存在  
    ImgObj.src = imgurl;
    //存在图片
    if (ImgObj.fileSize > 0 || (ImgObj.width > 0 && ImgObj.height > 0)) {
        return true;
    } else {
        return false;
    }
}
/**
 * 获得图片全路径
 * @param {Stirng|Base64String} _url 图片路径或base64字符串
 * @param {Number} _w 指定宽度
 * @param {Number} _h 指定高度
 */
function GetImgUrl(_url, _w, _h) {
    if (_url.length <= 0) {
        return '';
    }
    if (_url.indexOf('http') >= 0) {
        return _url
    }
    //判断是否为本地
    // if(_url.indexOf('../')>=0) {
    //     return require(_url)
    // }
    //判断是否为base64
    var reg = /^\s*data:([a-z]+\/[a-z0-9-+.]+(;[a-z-]+=[a-z0-9-]+)?)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@\/?%\s]*?)\s*$/i;
    if (reg.test(_url)) {
        return _url;
    }
    //判断最后一个字符是否存在/
    var basepath = config.img_url;
    if (basepath.charAt(basepath.length - 1) === "/") {
        basepath = basepath.substring(0, basepath.length - 1);
    }
    if (_w != undefined && _h != undefined) {
        if (_url.indexOf("/cutthumbs/") > 0) {
            return this.GetImgService() + _url;
        }
        _url = _url.replace("/upload/", "/upload/cutthumbs/");
        var index = _url.lastIndexOf("/");
        var front = _url.substring(0, index + 1);
        var back = _url.substring(index + 1);
        _url = front + "thumb_" + _w + "_" + _h + "_" + back;
        return basepath + _url;
    }
    return basepath + _url;
}

export default {
    CompressImg: CompressImg, //图片压缩
    dataURItoBlob: dataURItoBlob, //base64转blob
    dataURLtoFile: dataURLtoFile, //base64转file
    CheckImgExists: CheckImgExists, //判断图片是否存在
    GetImgUrl:GetImgUrl,//获得图片全路径
}